//
// Created by 19693 on 2023/11/28.
//
#include "stdlib.h"
#include "cJSON.h"
#include "analyse_cjson.h"
#include "stdio.h"

#define DATA_SIZE 6

void add_i2c_device_config(cJSON *json, i2c_transport_config *i2c_device_config) {
    char *str = "{\"command\":271,\"attach\":{\"channel\":1,\"info\":[{\"addr\":208,\"rate\":20000,\"rst\":1,\"data\":7}]}}";
    json = cJSON_Parse(str);
    cJSON *cjson_attach = NULL;
    int command;
    command = cJSON_GetObjectItem(json, "command")->valueint;        // 从json中获取键值内容
//    printf("command: %d\r\n",command);

    // 解析嵌套内容
    cjson_attach = cJSON_GetObjectItem(json, "attach");
    printf("analyse json\r\n");

    i2c_device_config->channel = cJSON_GetObjectItem(cjson_attach, "channel")->valueint;

    // 解析列表
    cJSON *info;
    info = cJSON_GetObjectItem(cjson_attach, "info");
    i2c_device_config->addr = cJSON_GetObjectItem(cJSON_GetArrayItem(info, 0), "addr")->valueint;
    i2c_device_config->rate = cJSON_GetObjectItem(cJSON_GetArrayItem(info, 0), "rate")->valueint;
    i2c_device_config->rst = cJSON_GetObjectItem(cJSON_GetArrayItem(info, 0), "rst")->valueint;
    i2c_device_config->data = cJSON_GetObjectItem(cJSON_GetArrayItem(info, 0), "data")->valueint;
    printf("data: %d rst: %d rate: %d channle: %d addr: %d\r\n", i2c_device_config->data, i2c_device_config->rst,
           i2c_device_config->rate, i2c_device_config->channel, i2c_device_config->addr);

    cJSON_Delete(cjson_attach);                                      // 释放cJSON_Parse()分配出来的内存空间

}


void recive_i2c_device_config(cJSON *json, i2c_scan_config *i2c_device_config) {

    // 测试字符串
    char *str = "{\"command\":272,\"attach\":{\"channel\":1,\"d_addr\":40,\"work\":{\"send\":[{\"addr\":3,\"data\":1}],\"listen\":[{\"addr\":32,\"length\":1},{\"addr\":33,\"length\":1},{\"addr\":34,\"length\":1}],\"s_delay\":1000,\"w_delay\":10}}}";
    json = cJSON_Parse(str);

    cJSON *cjson_attach = NULL;
    cJSON *cjson_work = NULL;
    int command;
    command = cJSON_GetObjectItem(json, "command")->valueint;        // 从json中获取键值内容

    // 解析嵌套内容,attach包
    cjson_attach = cJSON_GetObjectItem(json, "attach");
    printf("analyse json\r\n");
//    printf("channel: %d\r\n",cJSON_GetObjectItem(cjson_attach, "channel")->valueint);
    i2c_device_config->channel = cJSON_GetObjectItem(cjson_attach, "channel")->valueint;
    i2c_device_config->d_addr = cJSON_GetObjectItem(cjson_attach, "d_addr")->valueint;

    // 解析嵌套 work 内容
    cjson_work = cJSON_GetObjectItem(cjson_attach, "work");
    i2c_device_config->s_delay = cJSON_GetObjectItem(cjson_work, "s_delay")->valueint;
    i2c_device_config->w_delay = cJSON_GetObjectItem(cjson_work, "w_delay")->valueint;

    printf("channel: %d s_delay: %d w_delay: %d\r\n", i2c_device_config->channel, i2c_device_config->s_delay,
           i2c_device_config->w_delay);

    // 解析listen列表内容
    volatile cJSON *cjson_listen = NULL;
    cjson_listen = cJSON_GetObjectItem(cjson_work, "listen");
    int list_size = cJSON_GetArraySize(cjson_listen);
    printf("size: %d\r\n", list_size);
    for (int i = 0; i < list_size; ++i) {
        i2c_device_config->work_data[i].send.addr = cJSON_GetObjectItem(cJSON_GetArrayItem(cjson_listen, i),
                                                                        "addr")->valueint;
        i2c_device_config->work_data[i].send.data_length = cJSON_GetObjectItem(cJSON_GetArrayItem(cjson_listen, i),
                                                                               "length")->valueint;

    }


//    i2c_device_config->channle = cJSON_GetObjectItem(cjson_attach, "channle")->valueint;
//    i2c_device_config->addr = cJSON_GetObjectItem(cjson_attach, "d_addr")->valueint;
//    i2c_device_config->length = cJSON_GetObjectItem(cjson_attach, "d_addr")->valueint;
//    i2c_device_config->addr = cJSON_GetObjectItem(cjson_attach, "d_addr")->valueint;
//    i2c_device_config->addr = cJSON_GetObjectItem(cjson_attach, "d_addr")->valueint;
//    cJSON_Delete(cjson_attach);                                      // 释放cJSON_Parse()分配出来的内存空间

}

void send_data() {
    cJSON *cjson = NULL,*cjson_attach=NULL;
    cjson = cJSON_CreateObject();
    cJSON_AddItemToObject(cjson,"command", cJSON_CreateNumber(273));
    cjson_attach = cJSON_CreateArray();

    for (int i = 0; i < DATA_SIZE; ++i) {


    }
}
